package com.xsmart.common.entity;

import org.apache.commons.collections.MapUtils;
import org.springframework.http.HttpEntity;

import java.util.HashMap;
import java.util.Map;

public class RequestBean {
    private ResponseBean responseBean;
    /**
     * 请求来源，取值PC/MOBILE/WX
     */
    private String source;
    /**
     * 请求服务名称
     */
    private String serviceName;
    /**
     * 请求方法名称
     */
    private String methodName;
    /**
     * 登录验证，该值从登录接口获取
     */
    private String token;
    private Map<String, Object> privates;

    private HttpEntity<String> httpEntity;

    public HttpEntity<String> getHttpEntity() {
        return httpEntity;
    }

    public void setHttpEntity(HttpEntity<String> httpEntity) {
        this.httpEntity = httpEntity;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Map<String, Object> getPrivates() {
        return privates;
    }

    public void setPrivates(Map<String, Object> privates) {
        if (this.privates != null) {
            this.privates.putAll(privates);
        } else {
            this.privates = privates;
        }
    }

    public void addPrivatesParam(String name, Object value) {
        if (this.privates == null) {
            this.privates = new HashMap<>();
        }
        this.privates.put(name, value);
    }

    public Object getRequestParam(String paramName) {
        return MapUtils.getObject(privates, paramName);
    }

    public Object getRequestParam(String paramName, Object defaultValue) {
        return MapUtils.getObject(privates, paramName, defaultValue);
    }

    public String getRequestParamString(String paramName) {
        return MapUtils.getString(privates, paramName);
    }

    public String getRequestParamString(String paramName, String defaultValue) {
        return MapUtils.getString(privates, paramName, defaultValue);
    }

    public Integer getRequestParamInteger(String paramName) {
        return MapUtils.getInteger(privates, paramName);
    }

    public Integer getRequestParamInteger(String paramName, int defaultValue) {
        return MapUtils.getInteger(privates, paramName, defaultValue);
    }

    public Float getRequestParamFloat(String paramName){
        return MapUtils.getFloat(privates, paramName);
    }

    public Float getRequestParamFloat(String paramName, float defaultValue){
        return MapUtils.getFloat(privates, paramName, defaultValue);
    }

    public Long getRequestParamLong(String paramName) {
        return MapUtils.getLong(privates, paramName);
    }

    public Long getRequestParamLong(String paramName, long defaultValue) {
        return MapUtils.getLong(privates, paramName, defaultValue);
    }

    public ResponseBean getResponseBean() {
        if (responseBean == null) {
            responseBean = new ResponseBean();
            responseBean.setMethodName(this.getMethodName());
            responseBean.setServiceName(this.getServiceName());
            responseBean.setSource(this.getSource());
            responseBean.setToken(this.getToken());
            responseBean.setPrivates(this.getPrivates());
        }
        return responseBean;
    }

    public void setResponseBean(ResponseBean responseBean) {
        this.responseBean = responseBean;
    }
}
